<?php

namespace frontend\controllers;

/**
 * @author  shuang
 * @date    2016-10-26 13:54:29
 * @version V1.0
 * @desc    
 */
use common\components\WController;
use Yii;
use frontend\models\WB_Grade;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\helpers\Url;

class GradeController extends WController {

    public $message = "";
    public $flag = false;

    /*
     * 代理升级
     */

    public function actionLevel() {
        $res = \frontend\models\WB_User::getuserbase();
        if (!$res) {
            throw new \yii\web\NotFoundHttpException();
        }
        $data = WB_Grade::getList();
        return $this->render("level", ArrayHelper::merge($data, ["res" => $res]));
    }

    /*
     * 升级级别
     */

    public function actionUpgrade() {
        if (Yii::$app->request->isAjax) {
            $id = Yii::$app->request->post("id");
            $nametype = Yii::$app->request->post("nametype");
            $upgrade = Yii::$app->request->post("upgrade");
            $event = new \common\components\GradeEvent(["type" => $nametype, "_gradeid" => $id]);
            $this->on("upgrade_btnevnet", [$event, "allowupgrade"]);
            $this->trigger("upgrade_btnevnet", $event);
            $this->off("upgrade_btnevnet");
            if ($this->flag) {
                echo Json::encode(["status" => $this->flag, "msg" => $this->message, "url" => Url::toRoute(["account/pay", "nametype" => $nametype,"isupgrade"=>$upgrade])]);
            } else {
                echo Json::encode(["status" => $this->flag, "msg" => $this->message]);
            }
            exit;
        }
    }

}
